package com.knife.interfaces.extension.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.knife.common.utils.KnifeStringUtil;
import com.knife.interfaces.entity.KnifeInterfaceLogRecord;
import com.knife.interfaces.extension.config.AmapKeyProperties;
import com.knife.interfaces.extension.dto.AmapIpV1;
import com.knife.interfaces.extension.dto.AmapIpV2;
import com.knife.interfaces.extension.service.IpAddressService;
import com.knife.interfaces.mapper.KnifeInterfaceLogRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

import static com.knife.interfaces.extension.constants.KnifeInterfaceRocketMQConstant.*;

/**
 * @author tianhao.luo@hand-china.com
 * @version 1.0
 * @date 2022年03月09日 星期三 8:14 下午
 * @description
 * @since 1.0
 */
@Component
//@ConditionalOnBean(value = {ObjectMapper.class, IpAddressService.class,KnifeInterfaceLogRecordMapper.class}) 疑问：加了条件之后无法注入
@RocketMQMessageListener(
        // 指定和日志生产者一致的topic
        topic = INTERFACE_TOPIC,
        // 指定和yml中配置不一样的消费者组
        consumerGroup = INTERFACE_CONSUMER_GROUP,
        // 指定和日志生产者一致的tag
        selectorExpression = INTERFACE_CONSUMER_TAG,
        // 指定最大线程数
        consumeThreadMax = 8,
        replyTimeout = 6000)
public class KnifeInterfaceLogMessageConsumer implements RocketMQListener<String> {
    public static final Logger LOGGER = LoggerFactory.getLogger(KnifeInterfaceLogMessageConsumer.class);
    public static final String V_1 = "v1";
    public static final String V_2 = "v2";

    @Autowired
    private IpAddressService ipAddressService;
    @Autowired
    private KnifeInterfaceLogRecordMapper knifeInterfaceLogRecordMapper;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private AmapKeyProperties amapKeyProperties;


    @Override
    public void onMessage(String message) {
        LOGGER.debug("KnifeInterfaceLogMessageConsumer消费者----准备消费日志记录消息");
        KnifeInterfaceLogRecord knifeInterfaceLogRecordMapperByInvokeKey = knifeInterfaceLogRecordMapper.findByInvokeKey(message);
        if (Objects.nonNull(knifeInterfaceLogRecordMapperByInvokeKey) && amapKeyProperties.getEnableFlag()) {
            String active = amapKeyProperties.getActive();
            if (StringUtils.isNotEmpty(active)) {
                if (V_1.equals(active)) {
                    AmapIpV1 amapIpV1 = ipAddressService.obtainRealLocationV1(knifeInterfaceLogRecordMapperByInvokeKey.getIp());
                    if (Objects.nonNull(amapIpV1)) {
                        try {
                            knifeInterfaceLogRecordMapperByInvokeKey.setAttribute4(KnifeStringUtil.resolveStr(amapIpV1, amapKeyProperties.getV1().getTemplate()));
                            knifeInterfaceLogRecordMapperByInvokeKey.setAttribute5(objectMapper.writeValueAsString(amapIpV1));
                            knifeInterfaceLogRecordMapperByInvokeKey.setLastUpdateDate(new Date());
                            knifeInterfaceLogRecordMapper.saveAndFlush(knifeInterfaceLogRecordMapperByInvokeKey);
                            LOGGER.debug("KnifeInterfaceLogMessageConsumer消费者----消费日志记录消息成功");
                        } catch (Exception e) {
                            LOGGER.error("更新IP地址失败" + e.getMessage());
                        }
                    }
                } else if (V_2.equals(active)) {
                    AmapIpV2 amapIpV2 = ipAddressService.obtainRealLocationV2(knifeInterfaceLogRecordMapperByInvokeKey.getIp());
                    if (Objects.nonNull(amapIpV2)) {
                        try {
                            knifeInterfaceLogRecordMapperByInvokeKey.setAttribute4(KnifeStringUtil.resolveStr(amapIpV2, amapKeyProperties.getV2().getTemplate()));
                            knifeInterfaceLogRecordMapperByInvokeKey.setAttribute5(objectMapper.writeValueAsString(amapIpV2));
                            knifeInterfaceLogRecordMapperByInvokeKey.setLastUpdateDate(new Date());
                            knifeInterfaceLogRecordMapper.saveAndFlush(knifeInterfaceLogRecordMapperByInvokeKey);
                            LOGGER.debug("KnifeInterfaceLogMessageConsumer消费者----消费日志记录消息成功");
                        } catch (Exception e) {
                            LOGGER.error("更新IP地址失败" + e.getMessage());
                        }
                    }
                }
            }
        }
    }
}
